﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RpcGenerator.CSharp
{
    class MessageJsonGenerator
    {
        public void Generate(MessageDescription description, IMessageSerializer serializer)
        {
            serializer.NewLine();
            serializer.NewLine("[DataContract]");
            serializer.NewLine(string.Format("class {0} : IMessage", description.Name));
            serializer.BeginBlock();
            {
                WriteMessageId(description, serializer);
                WriteFields(description, serializer);

                GenerateReadFunction(description, serializer);
                GenerateWriteFunction(description, serializer);
            }
            serializer.EndBlock();
        }

        private void WriteMessageId(MessageDescription description, IMessageSerializer serializer)
        {
            serializer.NewLine("public Int32 MessageId { get{ return ");
            serializer.Apppend(description.MessageId.ToString());
            serializer.Apppend(";}}");

            serializer.NewLine("public UInt16  Flags     { get;set;}");
            serializer.NewLine("public Int32   Sessionid { get;set;}");
            serializer.NewLine("public Int32   OpCode    { get;set;}");
        }

        private void WriteFields(MessageDescription description, IMessageSerializer serializer)
        {
            serializer.NewLine();
            foreach (MessageFieldDescription mfd in description.Fields)
            {
                serializer.NewLine("[DataMember]");
                if (mfd.IsGenericType())
                {
                    serializer.NewLine(string.Format("public {0} {1};", mfd.GenericTypeName, mfd.Name));
                }
                else
                {
                    serializer.NewLine(string.Format("public {0} {1};", mfd.Type.Name, mfd.Name));
                }
            }
        }

        private void GenerateReadFunction(MessageDescription description, IMessageSerializer serializer)
        {
            serializer.NewLine();
            serializer.NewLine("public void Read(NetStream ns)");
            serializer.BeginBlock();
            {
            }
            serializer.EndBlock();

            serializer.NewLine();
            serializer.NewLine(string.Format("public static {0} ReadFromJson(NetStream ns)",description.Name));
            serializer.BeginBlock();
            {
                serializer.NewLine("string json = ns.ReadString();");
                serializer.NewLine(string.Format("return JsonHelper.ParseFromJson<{0}>(json);", description.Name));
            }
            serializer.EndBlock();
        }

        private void GenerateWriteFunction(MessageDescription description, IMessageSerializer serializer)
        {
            serializer.NewLine();
            serializer.NewLine("public void Write(NetStream ns)");
            serializer.BeginBlock();
            {
                serializer.NewLine(string.Format("string json = JsonHelper.GetJson<{0}>(this);",description.Name));
                serializer.NewLine("ns.WriteString(json);");
            }
            serializer.EndBlock();
        }
    }
}
